package com.example.service.impl;

import com.example.mapper.FoodMapper;
import com.example.pojo.Food;
import com.example.pojo.PageBeans;
import com.example.service.FoodService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    /*
   分页查询--餐饮菜单
    */
/*    @Override
    public PageBeans select(Integer page,Integer pageSize) {
       Long total = foodMapper.selectByTotal();//获取总记录数
       List<Food> list = foodMapper.selectByRows((page-1)*pageSize,pageSize);//获取结果列表 注意这个是页码，不是起始索引，要传过去的应该是起始索引
        return new PageBeans(total,list);
    }*/

    //分页查询--使用分页插件
    @Override
    public PageBeans list(Integer page,Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
       List<Food> list =  foodMapper.list();
        Page<Food> p = (Page<Food>)list;
        //封装结果并返回
        PageBeans pageBeans = new PageBeans(p.getTotal(),p.getResult());
        return pageBeans;
    }

    @Override
    public void preserve(int id, int number) {
        foodMapper.preserve(id,number);
    }
}
